/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.IncomeBillBean;
import com.minierp.bean.QueryParameterBean;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.IncomeBillDao;
import com.minierp.dao.IncomeTypeDao;
import com.minierp.dao.PayModeDao;
import com.minierp.dao.QueryDao;
import com.minierp.dao.TallySetDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncIncomeBillMACtrHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.BankAccount;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.IncomeBill;
import com.minierp.model.TallySet;
import com.minierp.service.IncomeBillService;
import com.minierp.view.IncomeBillListExcelView;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class IncomeBillMACtr extends MultiActionController {
	private String dialogSuccessView;
	private String listForExamineView;
	private String infoForExamineView;
	private String listForUnExamineView;
	private String infoForUnExamineView;
	private String searchResultView;
	private String listForCancelView;
	private String infoForCancelView;
	private String printView;
	private IncomeBillDao incomeBillDao;
	private DepartmentDao departmentDao;
	private BankAccountDao bankAccountDao;
	private BankAccountBalanceDao bankAccountBalanceDao;
	private BankAccountDetailDao bankAccountDetailDao;
	private PayModeDao payModeDao;
	private IncomeTypeDao incomeTypeDao;
	private TallySetDao tallySetDao;
	private QueryDao queryDao;
	private CodeRuleDao codeRuleDao;
	private IdGenerator idGenerator;
	private IncomeBillService incomeBillService;
	private DataSource dataSource;

	public ModelAndView handleExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.dialogSuccessView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String idStr = request.getParameter("id_str");
		String flag = request.getParameter("flag");
		boolean examined = false;
		if (flag != null && flag.equals("1")) {
			examined = true;
		}

		String[] arr = idStr.split(";");
		boolean num = false;
		String msg;
		byte arg16;
		if (!examined) {
			if (!EncCommonHelper.checkPrivilege(request, "INCO_UN_EXAM")) {
				throw new Exception("Permission deny!");
			}

			try {
				for (int examinerId = 0; examinerId < arr.length; ++examinerId) {
					this.incomeBillService.txUnExamine(
							Long.parseLong(arr[examinerId]),
							this.incomeBillDao, this.bankAccountBalanceDao,
							this.bankAccountDetailDao, this.idGenerator, ub);
				}

				msg = "反审核成功";
			} catch (CannotAcquireLockException arg15) {
				msg = "事务冲突故障，请重新操作！";
			}

			arg16 = 1;
		} else {
			if (!EncCommonHelper.checkPrivilege(request, "INCO_EXAM")) {
				throw new Exception("Permission deny!");
			}

			long arg17 = ub.getStaffer().getId();
			String examinerName = ub.getStaffer().getStafferName();

			try {
				for (int w = 0; w < arr.length; ++w) {
					this.incomeBillService.txExamine(Long.parseLong(arr[w]),
							this.incomeBillDao, arg17, examinerName,
							this.bankAccountBalanceDao, this.idGenerator);
				}

				msg = "审核成功";
			} catch (CannotAcquireLockException arg14) {
				msg = "事务冲突故障，请重新操作！";
			}

			arg16 = 1;
		}

		mv.addObject("returnValue", String.valueOf(arg16));
		EncCommonHelper.setAlertMessage(request, msg);
		return mv;
	}

	public ModelAndView handleListForExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "INCO_EXAM")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listForExamineView);
			String searchKey = request.getParameter("search_key");
			String tsId = request.getParameter("ts_id");
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			if (tsId == null || tsId.equals("")) {
				tsId = String.valueOf(ub.getUser().getDfTsId());
			}

			Date allowStart = null;
			if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
				allowStart = EncCommonHelper.getEarlDataDate(request);
			}

			List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
			int perPageNum = EncCommonHelper.getPageSize(request);
			int total = this.incomeBillDao.getListCount(searchKey, false,
					Long.parseLong(tsId), ub, allowStart);
			int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
					perPageNum);
			int startIndex = (pageno - 1) * perPageNum;
			List list = this.incomeBillDao.getList(searchKey, false,
					Long.parseLong(tsId), ub, allowStart, startIndex,
					perPageNum);
			mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
					perPageNum, pageno));
			mv.addObject("searchKey", searchKey);
			mv.addObject("tsId", tsId);
			mv.addObject("tsList", tsList);
			return mv;
		}
	}

	public ModelAndView handleInfoForExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForExamineView);
		String id = request.getParameter("id");
		this.getIncomeBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleListForUnExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "INCO_UN_EXAM")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listForUnExamineView);
			String searchKey = request.getParameter("search_key");
			String tsId = request.getParameter("ts_id");
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			if (tsId == null || tsId.equals("")) {
				tsId = String.valueOf(ub.getUser().getDfTsId());
			}

			Date allowStart = null;
			if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
				allowStart = EncCommonHelper.getEarlDataDate(request);
			}

			List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
			int perPageNum = EncCommonHelper.getPageSize(request);
			int total = this.incomeBillDao.getListCount(searchKey, true,
					Long.parseLong(tsId), ub, allowStart);
			int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
					perPageNum);
			int startIndex = (pageno - 1) * perPageNum;
			List list = this.incomeBillDao.getList(searchKey, true,
					Long.parseLong(tsId), ub, allowStart, startIndex,
					perPageNum);
			mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
					perPageNum, pageno));
			mv.addObject("searchKey", searchKey);
			mv.addObject("tsId", tsId);
			mv.addObject("tsList", tsList);
			return mv;
		}
	}

	public ModelAndView handleInfoForUnExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForUnExamineView);
		String id = request.getParameter("id");
		this.getIncomeBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleListForCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "INCO_CANC")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.listForCancelView);
			String searchKey = request.getParameter("search_key");
			String tsId = request.getParameter("ts_id");
			UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
			if (tsId == null || tsId.equals("")) {
				tsId = String.valueOf(ub.getUser().getDfTsId());
			}

			Date allowStart = null;
			if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
				allowStart = EncCommonHelper.getEarlDataDate(request);
			}

			List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
			int perPageNum = EncCommonHelper.getPageSize(request);
			int total = this.incomeBillDao.getListCount(searchKey,
					Long.parseLong(tsId), ub, allowStart);
			int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
					perPageNum);
			int startIndex = (pageno - 1) * perPageNum;
			List list = this.incomeBillDao.getList(searchKey,
					Long.parseLong(tsId), ub, allowStart, "", startIndex,
					perPageNum);
			mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
					perPageNum, pageno));
			mv.addObject("searchKey", searchKey);
			mv.addObject("tsId", tsId);
			mv.addObject("tsList", tsList);
			return mv;
		}
	}

	public ModelAndView handleInfoForCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForCancelView);
		String id = request.getParameter("id");
		this.getIncomeBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.dialogSuccessView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (!EncCommonHelper.checkPrivilege(request, "INCO_CANC")) {
			throw new Exception("Permission deny!");
		} else {
			String id = request.getParameter("id");
			String reason = request.getParameter("reason");

			String msg;
			try {
				this.incomeBillService.txCancel(Long.parseLong(id), reason,
						this.incomeBillDao, ub, this.bankAccountBalanceDao,
						this.bankAccountDetailDao, this.idGenerator);
				msg = "作废成功！";
			} catch (CannotAcquireLockException arg8) {
				msg = "事务冲突故障，请重新操作！";
			}

			mv.addObject("returnValue", id);
			EncCommonHelper.setAlertMessage(request, msg);
			return mv;
		}
	}

	private void getIncomeBill(ModelAndView mv, long id) {
		IncomeBill incomeBill = (IncomeBill) this.incomeBillDao.get(
				IncomeBill.class, id);
		if (incomeBill != null && incomeBill.getTallySetId() != 0L) {
			mv.addObject(
					"tallySet",
					this.incomeBillDao.get(TallySet.class,
							incomeBill.getTallySetId()));
		}

		mv.addObject("record", incomeBill);
		if (incomeBill.getAttachFile() != null
				&& !incomeBill.getAttachFile().equals("")) {
			int beginIndex = incomeBill.getAttachFile().lastIndexOf("/");
			mv.addObject("attFile",
					incomeBill.getAttachFile().substring(beginIndex + 1));
		}

		mv.addObject("bankAccountList", this.bankAccountDao.getList("", ""));
		mv.addObject("departmentList", this.departmentDao.getList(true));
		mv.addObject("incomeTypeList", this.incomeTypeDao.getList(false));
	}

	public ModelAndView handleSearchResult(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		if (!EncCommonHelper.checkPrivilege(request, "INCO_BILL_SEAR")) {
			throw new PermissionDennyException("Permission Denny");
		} else {
			ModelAndView mv = new ModelAndView(this.searchResultView);
			String contentType = request.getParameter("content_type");
			List bankAccountList = this.bankAccountDao.getList("", "");
			List depList = this.departmentDao.getList(true);
			List incomeTypeList = this.incomeTypeDao.getList(false);
			QueryParameterBean b = EncIncomeBillMACtrHelper.handleSearchResult(
					request, this.dataSource);
			String[] countObjs = new String[]{"sum(b.cost)"};
			QueryResult qr;
			if (contentType != null && contentType.equals("EXCEL")) {
				qr = this.queryDao.doQuery(b.getQParas(), b.getOParas(),
						(String[]) null, b.getShortNameMap(), b.getRetObjs(),
						countObjs);
				IncomeBillListExcelView arg17 = new IncomeBillListExcelView();
				HashMap arg19 = new HashMap();
				arg19.put("list", qr.getList());
				arg19.put("baList", bankAccountList);
				arg19.put("depList", depList);
				arg19.put("itList", incomeTypeList);
				arg19.put("incomeTotal", qr.getExtCount()[0]);
				return new ModelAndView(arg17, arg19);
			} else {
				qr = this.queryDao.doQuery(b.getCurrentPage(), b.getPageSize(),
						b.getQParas(), b.getOParas(), (String[]) null,
						b.getShortNameMap(), b.getRetObjs(), countObjs);
				ArrayList arr = new ArrayList();

				for (int ent = 0; ent < qr.getList().size(); ++ent) {
					IncomeBillBean ub = new IncomeBillBean();
					IncomeBill bill = (IncomeBill) qr.getList().get(ent);
					ub.setIncomeBill(bill);
					String baName = "";
					if (bill.getBankAccountId() != 0L) {
						for (int j = 0; j < bankAccountList.size(); ++j) {
							BankAccount ba = (BankAccount) bankAccountList
									.get(j);
							if (ba.getId() == bill.getBankAccountId()) {
								baName = ba.getBankName();
								if (ba.getAccountName() != null
										&& !ba.getAccountName().equals("")) {
									baName = baName + "(" + ba.getAccountName()
											+ ")";
								}
							}
						}
					}

					ub.setBankAccountName(baName);
					arr.add(ub);
				}

				qr.setList(arr);
				mv.addAllObjects(b.getParameters());
				mv.addObject("qr", qr);
				mv.addObject("incomeTotal", qr.getExtCount()[0]);
				mv.addObject("depList", depList);
				mv.addObject("itList", incomeTypeList);
				EnterpriseInfo arg18 = EncCommonHelper
						.getEnterpriseInfo(request);
				UserBean arg20 = EncCommonHelper.getCurrentUserAccount(request);
				mv.addObject("myBaList", PageHelper.parseBaList(
						bankAccountList, arg18, arg20, 0L, 0L));
				mv.addObject("tsList",
						PageHelper.getTallySetList(this.tallySetDao, arg20));
				return mv;
			}
		}
	}

	public ModelAndView handlePrint(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.printView);
		String id = request.getParameter("id");
		this.getIncomeBill(mv, Long.parseLong(id));
		mv.addObject("codeRule", this.codeRuleDao.get("INCOME_BILL"));
		mv.addObject("retVal", request.getParameter("ret_val"));
		return mv;
	}

	public String getDialogSuccessView() {
		return this.dialogSuccessView;
	}

	public void setDialogSuccessView(String dialogSuccessView) {
		this.dialogSuccessView = dialogSuccessView;
	}

	public IncomeBillDao getIncomeBillDao() {
		return this.incomeBillDao;
	}

	public void setIncomeBillDao(IncomeBillDao incomeBillDao) {
		this.incomeBillDao = incomeBillDao;
	}

	public String getInfoForExamineView() {
		return this.infoForExamineView;
	}

	public void setInfoForExamineView(String infoForExamineView) {
		this.infoForExamineView = infoForExamineView;
	}

	public String getInfoForUnExamineView() {
		return this.infoForUnExamineView;
	}

	public void setInfoForUnExamineView(String infoForUnExamineView) {
		this.infoForUnExamineView = infoForUnExamineView;
	}

	public String getListForExamineView() {
		return this.listForExamineView;
	}

	public void setListForExamineView(String listForExamineView) {
		this.listForExamineView = listForExamineView;
	}

	public String getListForUnExamineView() {
		return this.listForUnExamineView;
	}

	public void setListForUnExamineView(String listForUnExamineView) {
		this.listForUnExamineView = listForUnExamineView;
	}

	public String getSearchResultView() {
		return this.searchResultView;
	}

	public void setSearchResultView(String searchResultView) {
		this.searchResultView = searchResultView;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public String getInfoForCancelView() {
		return this.infoForCancelView;
	}

	public void setInfoForCancelView(String infoForCancelView) {
		this.infoForCancelView = infoForCancelView;
	}

	public String getListForCancelView() {
		return this.listForCancelView;
	}

	public void setListForCancelView(String listForCancelView) {
		this.listForCancelView = listForCancelView;
	}

	public IncomeBillService getIncomeBillService() {
		return this.incomeBillService;
	}

	public void setIncomeBillService(IncomeBillService incomeBillService) {
		this.incomeBillService = incomeBillService;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public BankAccountBalanceDao getBankAccountBalanceDao() {
		return this.bankAccountBalanceDao;
	}

	public void setBankAccountBalanceDao(
			BankAccountBalanceDao bankAccountBalanceDao) {
		this.bankAccountBalanceDao = bankAccountBalanceDao;
	}

	public BankAccountDetailDao getBankAccountDetailDao() {
		return this.bankAccountDetailDao;
	}

	public void setBankAccountDetailDao(
			BankAccountDetailDao bankAccountDetailDao) {
		this.bankAccountDetailDao = bankAccountDetailDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public BankAccountDao getBankAccountDao() {
		return this.bankAccountDao;
	}

	public void setBankAccountDao(BankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}

	public IncomeTypeDao getIncomeTypeDao() {
		return this.incomeTypeDao;
	}

	public void setIncomeTypeDao(IncomeTypeDao incomeTypeDao) {
		this.incomeTypeDao = incomeTypeDao;
	}

	public PayModeDao getPayModeDao() {
		return this.payModeDao;
	}

	public void setPayModeDao(PayModeDao payModeDao) {
		this.payModeDao = payModeDao;
	}

	public TallySetDao getTallySetDao() {
		return this.tallySetDao;
	}

	public void setTallySetDao(TallySetDao tallySetDao) {
		this.tallySetDao = tallySetDao;
	}

	public String getPrintView() {
		return this.printView;
	}

	public void setPrintView(String printView) {
		this.printView = printView;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}
}